import Vue from 'vue'
import Router from 'vue-router'
import main from '@/views/main.vue'
import login from '@/views/login.vue'
import categoryList from '@/views/categoryList.vue'
import categoryEdit from '@/views/categoryEdit.vue'
import itemEdit from '@/views/itemEdit.vue'
import itemList from '@/views/itemList.vue'
import heroEdit from '@/views/heroEdit.vue'
import heroList from '@/views/heroList.vue'
import articleList from '@/views/articleList.vue'
import articleEdit from '@/views/articleEdit.vue'
import adList from '@/views/adList.vue'
import adEdit from '@/views/adEdit.vue'
import userList from '@/views/userList.vue'
Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/login', name: 'login', component: login, meta: { isPublic: true } },
    {
      path: '/',
      name: 'main',
      component: main,
      children: [
        {
          path: '/categories/create', component: categoryEdit
        },
        {
          name: 'categoriesedit',
          path: '/categories/edit/:id', component: categoryEdit, props: true
        },
        {
          path: '/categories/list', component: categoryList
        },
        {
          path: '/item/create', component: itemEdit
        },
        {
          name: 'itemedit',
          path: '/item/edit/:id', component: itemEdit, props: true
        },
        {
          path: '/item/list', component: itemList
        },
        {
          path: '/hero/create', component: heroEdit
        },
        {
          name: 'heroedit',
          path: '/hero/edit/:id', component: heroEdit, props: true
        },
        {
          path: '/hero/list', component: heroList
        },
        {
          path: '/articles/create', component: articleEdit
        },
        {
          name: 'articlesedit',
          path: '/articles/edit/:id', component: articleEdit, props: true
        },
        {
          path: '/articles/list', component: articleList
        },
        {
          path: '/item/create', component: itemEdit
        },
        {
          path: '/ads/create', component: adEdit
        },
        {
          name: 'adsedit',
          path: '/ads/edit/:id', component: adEdit, props: true
        },
        {
          path: '/ads/list', component: adList
        },
        {
          path: '/user/list', component: userList
        },
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !sessionStorage.token) {
    return next('/login')
  }
  next()
})
export default router